package com.example.teacherservice.controller;

import com.example.teacherservice.dto.*;
import com.example.teacherservice.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

// QuestionController.java
@RestController
@RequestMapping("/api/questions")
public class QuestionController {
    @Autowired
    public QuestionService questionService;


    public QuestionController(QuestionService questionService) {
        this.questionService = questionService;
    }

    @PostMapping
    public ResponseEntity<QuestionDTO> createQuestion(@RequestBody QuestionRequest request) {
        QuestionDTO question = questionService.createQuestion(request);
        return ResponseEntity.ok(question);
    }

    @GetMapping("/{id}")
    public ResponseEntity<QuestionDTO> getQuestionById(@PathVariable Long id) {
        return ResponseEntity.ok(questionService.getQuestionById(id));
    }

    @GetMapping
    public ResponseEntity<List<QuestionDTO>> getAllQuestions() {
        return ResponseEntity.ok(questionService.getAllQuestions());
    }

    @GetMapping("/user/student/{studentId}")
    public ResponseEntity<List<QuestionDTO>> getQuestionsByStudentId(@PathVariable Long studentId) {
        return ResponseEntity.ok(questionService.getQuestionsByStudentId(studentId));
    }

    @GetMapping("/user/teacher/{teacherId}")
    public ResponseEntity<List<QuestionDTO>> getQuestionsByTeacherId(@PathVariable Long teacherId) {
        return ResponseEntity.ok(questionService.getQuestionsByTeacherId(teacherId));
    }

    @GetMapping("/class/{classId}")
    public ResponseEntity<List<QuestionDTO>> getQuestionsByClassId(@PathVariable Long classId) {
        return ResponseEntity.ok(questionService.getQuestionsByClassId(classId));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteQuestion(@PathVariable Long id) {
        questionService.deleteQuestion(id);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/comments")
    public ResponseEntity<CommentDTO> addComment(@RequestBody CommentRequest request) {
        CommentDTO comment = questionService.addComment(request);
        return ResponseEntity.ok(comment);
    }

    @PutMapping("/comments/{commentId}/adopt")
    public ResponseEntity<CommentDTO> adoptComment(@PathVariable Long commentId) {
        return ResponseEntity.ok(questionService.adoptComment(commentId));
    }

    @DeleteMapping("/comments/{commentId}")
    public ResponseEntity<Void> deleteComment(@PathVariable Long commentId) {
        questionService.deleteComment(commentId);
        return ResponseEntity.noContent().build();
    }


    @PostMapping("/{questionId}/collect")
    public ResponseEntity<Void> collectQuestion(
            @PathVariable Long questionId,
            @RequestBody CollectionRequest request) {
        questionService.collectQuestion(questionId, request);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{questionId}/comments")
    public ResponseEntity<List<CommentDTO>> getCommentsByQuestionId(@PathVariable Long questionId) {
        return ResponseEntity.ok(questionService.getCommentsByQuestionId(questionId));
    }

    @GetMapping("/questions")
    public ResponseEntity<List<UserQuestionDTO>> getUserQuestions(
            @RequestParam Long userId) {
        return ResponseEntity.ok(questionService.getUserQuestions(userId));
    }

    @GetMapping("/comments")
    public ResponseEntity<List<UserCommentDTO>> getUserComments(
            @RequestParam Long userId) {
        return ResponseEntity.ok(questionService.getUserComments(userId));
    }

    @DeleteMapping("/questions")
    public ResponseEntity<Void> deleteUserQuestion(
            @RequestParam Long userId,
            @RequestParam Long questionId) {
        try {
            questionService.deleteUserQuestion(questionId, userId);
            return ResponseEntity.noContent().build();
        } catch (RuntimeException e) {
            throw new ResponseStatusException(
                    HttpStatus.FORBIDDEN, e.getMessage(), e);
        }
    }


    @DeleteMapping("/comments")
    public ResponseEntity<Void> deleteUserComment(
            @RequestParam Long userId,
            @RequestParam Long commentId) {
        try {
            questionService.deleteUserComment(commentId, userId);
            return ResponseEntity.noContent().build();
        } catch (RuntimeException e) {
            throw new ResponseStatusException(
                    HttpStatus.FORBIDDEN, e.getMessage(), e);
        }
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<QuestionDTO>> getQuestionsByUserId(@PathVariable Long userId) {
        return ResponseEntity.ok(questionService.getQuestionsByUserId(userId));
    }
}